package tech.yunland.shop.domain.handler;

import com.moyuer.bsf.domain.model.Organ;
import com.moyuer.bsf.domain.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import tech.yunland.shop.domain.model.GoodsPackage;
import tech.yunland.shop.domain.service.GoodsPackageService;
import tech.yunland.weixin.domain.model.OrganWexin;
import tech.yunland.weixin.domain.service.OrganWexinService;
import tech.yunland.weixin.handler.MessageHandler;
import tech.yunland.weixin.protocol.BaseArticle;
import tech.yunland.weixin.protocol.BaseMessage;
import tech.yunland.weixin.protocol.send.reply.NewsReplyMsg;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author 李珂<likenamehaojie@hotmail.com>
 */
@Component
public class GoodsPackageMessageHandler implements MessageHandler<BaseMessage> {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserService userService;

    @Autowired
    private OrganWexinService organWexinService;

    @Autowired
    private GoodsPackageService goodsPackageService;
    //http://like.s1.natapp.cc/#/goods-package-weixin
    @Value("${weixin.api.host_url}")
    private String host_url;

    @Override
    public NewsReplyMsg deal(BaseMessage inputMessage) {

        NewsReplyMsg result = new NewsReplyMsg();
        result.setFromUser(inputMessage.getToUser());
        result.setToUser(inputMessage.getFromUser());
        result.setCreateTime(new Date());
        result.setType(BaseMessage.MsgType.news);

        result.setArticles(this.getBaseArticle(inputMessage));
        result.setArticleCount(this.getBaseArticle(inputMessage).size());

        return result;
    }

    public List<BaseArticle> getBaseArticle(BaseMessage inputMessage) {

        List<BaseArticle> articleList = new ArrayList<BaseArticle>();

        BaseArticle art = new BaseArticle();
        art.setTitle("优惠活动");
        //art.setUrl("http://api.yunland.tech/public/weixin/#/charge/goods-package?openId=" + inputMessage.getFromUser());
        OrganWexin organWexin = organWexinService.getBywxNumber(inputMessage.getToUser());
        Organ organ = organWexin.getOwner();
        String organId = organ.getId();
        art.setUrl(host_url + "/#/goods-package-weixin?organId="+organId);
        articleList.add(art);
        return articleList;
    }

}

